import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class IndStatsParser {
	
	static int type;
	static int temp;
	static int NEW_PLAYER = 1;
	static IndStats playerStats;
	static FileWriter writer;
	static boolean team = false;

	public static void main(String[] args) {
		type = 0;
		temp = 100;
		
		try {
			writer = new FileWriter("C:\\Users\\Dallin\\Downloads\\2011 BYU\\AEX-INDSTATS-BYU.csv");
			writer.append("Name, ID, Games, Minutes, PPG, RPG, APG, OffReb, DefReb, TotReb, Assists, Steals, Blocks," +
					" Turnovers, Fouls, FGMade, FGAtt, FG%, 3Made, 3Att, 3%, FTMade, FTAtt, FT%, Points, Highest Point");
			writer.append('\n');
			 
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
		 
			DefaultHandler handler = new DefaultHandler() {
			 
				public void startElement(String uri, String localName,String qName, 
			                Attributes attributes) throws SAXException {

					temp++;
					if (qName.equalsIgnoreCase("Player")) { 
						playerStats = new IndStats();
						playerStats.playerName(attributes.getValue("name"));
						temp = 0;
					}
					else if (qName.equalsIgnoreCase("Team_Totals")) { 
						playerStats = new IndStats();
						playerStats.playerName("Team");
						temp = 0;
						team = true;
					}
					type = temp;
				}
			 
				public void endElement(String uri, String localName, String qName) throws SAXException {
			 
					type = 0;
					if (qName.equalsIgnoreCase("Player")) { 
						System.out.println(playerStats); 
						try {
							writer.append(playerStats.csv());
							writer.append('\n');
						} catch (IOException e) {
							e.printStackTrace();
						} 
					}
					if (qName.equalsIgnoreCase("Team_Totals")) { 
						System.out.println(playerStats); 
						try {
							writer.append(playerStats.csv());
							writer.append('\n');
						} catch (IOException e) {
							e.printStackTrace();
						} 
					}
			 
				}
			 
				public void characters(char ch[], int start, int length) throws SAXException {
			 
					if(team){
						if(type<3){
							if (type == 1) { playerStats.gamesPlayed(Integer.parseInt(new String(ch, start, length))); }
							else if (type == 2) { playerStats.minutesPlayed(Integer.parseInt(new String(ch, start, length))); }
							return;
						}
						if(type>2)
							type += 4;
					}
					if (type == 1) { playerStats.playerID(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 2) { playerStats.gamesPlayed(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 3) { playerStats.minutesPlayed(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 4) { playerStats.ppg(Double.parseDouble(new String(ch, start, length))); }
					else if (type == 5) { playerStats.rpg(Double.parseDouble(new String(ch, start, length))); }
					else if (type == 6) { playerStats.apg(Double.parseDouble(new String(ch, start, length))); }
					else if (type == 7) { playerStats.offReb(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 8) { playerStats.defReb(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 9) { playerStats.totReb(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 10) { playerStats.assists(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 11) { playerStats.steals(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 12) { playerStats.blocks(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 13) { playerStats.turnovers(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 14) { playerStats.fouls(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 15) { playerStats.fgmade(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 16) { playerStats.fgattempted(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 17) { playerStats.fgper(Double.parseDouble(new String(ch, start, length))); }
					else if (type == 18) { playerStats.threesmade(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 19) { playerStats.threesattempted(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 20) { playerStats.threesper(Double.parseDouble(new String(ch, start, length))); }
					else if (type == 21) { playerStats.ftmade(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 22) { playerStats.ftattempted(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 23) { playerStats.ftper(Double.parseDouble(new String(ch, start, length))); }
					else if (type == 24) { playerStats.points(Integer.parseInt(new String(ch, start, length))); }
					else if (type == 25) { if(!team)playerStats.highPoints(Integer.parseInt(new String(ch, start, length))); }
					
				}
		 
		    };
		 
		    saxParser.parse("C:\\Users\\Dallin\\Downloads\\2011 BYU\\AEX-INDSTATS-BYU.xml", handler);
		    
		    writer.flush();
		    writer.close();
		    
		    } catch (Exception e) {
		    	e.printStackTrace();
		    }	
		
	}
}
